package com.gungnir.trial.impl;

import com.gungnir.trial.ThreeRateService;
import com.gungnir.trial.impl.supervision.KeduServiceImpl;
import com.tyrfing.model.Pagination;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import com.gungnir.trial.entity.kedu.KeduRoomDetailVO;
import java.util.List;
import java.time.Instant;

@Component
public class TreeRateImpl {
    private final KeduServiceImpl keduServiceImpl;
    private final ThreeRateService threeRateService;

    public TreeRateImpl(KeduServiceImpl keduServiceImpl, ThreeRateService threeRateService) {
        this.keduServiceImpl = keduServiceImpl;
        this.threeRateService = threeRateService;
    }

    // 每10分钟执行一次
    @Scheduled(fixedRate = 600000)
    public void callKeduRoomDetail() {
        // 获取当前 UTC 时间
        long currentTime = Instant.now().toEpochMilli();
        long tenMinutesAgo = currentTime - TimeUnit.MINUTES.toMillis(10);

        System.out.println("Scheduled task started: callKeduRoomDetail currentTime(UTC): "
                + Instant.ofEpochMilli(currentTime)
                + " tenMinutesAgo(UTC): "
                + Instant.ofEpochMilli(tenMinutesAgo));

        try {
            Pagination<KeduRoomDetailVO> result = keduServiceImpl.pageKeduRoomDetail(100, 1, "", tenMinutesAgo, currentTime);

            List<KeduRoomDetailVO> roomDetails = result.getData();
            if (roomDetails != null) {
                for (KeduRoomDetailVO detail : roomDetails) {
                    // 获取需要的参数
                    int shouldAttendance = detail.getCourseEnrollmentCount() != null ? detail.getCourseEnrollmentCount() : 60; // 选课人数
                    int attendanceCount = detail.getAttendanceCount() != null ? detail.getAttendanceCount() : 0; // 到课人数
                    int attendanceFaceCount = detail.getAttendanceFaceCount() != null ? detail.getAttendanceFaceCount() : 0; // 抬头人数
                    int frontAttendancetCount = detail.getFrontAttendancetCount() != null ? detail.getFrontAttendancetCount() : 0; // 前排到课人数
                    int clroFrontSeatCount = detail.getClroFrontSeatCount() != null ? detail.getClroFrontSeatCount() : 0; // 前排座位数
                    String classRoomCode = detail.getClroCode();
                    String teachingClassCode = detail.getTeachingClassCode() != null ? detail.getTeachingClassCode() : "";
                    Long imagShotTime = detail.getImagShotTime();

                    threeRateService.countAndInsertThreeRate(
                            shouldAttendance,
                            attendanceFaceCount,
                            attendanceCount,
                            frontAttendancetCount,
                            clroFrontSeatCount,
                            classRoomCode,
                            teachingClassCode,
                            imagShotTime
                    );

                    System.out.println("Computed and stored three rates for classRoomCode: " + classRoomCode);
                }
            } else {
                System.out.println("No data found");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 计算到课率
    private double calculateAttendanceRate(KeduRoomDetailVO detail) {
        if (detail.getClroSeatCount() != null && detail.getAttendanceCount() != null && detail.getClroSeatCount() > 0) {
            return (double) detail.getAttendanceCount() / detail.getClroSeatCount();
        }
        return 0;
    }

    // 计算前排率
    private double calculateFrontSeatRate(KeduRoomDetailVO detail) {
        if (detail.getClroFrontSeatCount() != null && detail.getFrontAttendancetCount() != null && detail.getClroFrontSeatCount() > 0) {
            return (double) detail.getFrontAttendancetCount() / detail.getClroFrontSeatCount();
        }
        return 0;
    }

    // 计算抬头率
    private double calculateFaceRate(KeduRoomDetailVO detail) {
        if (detail.getAttendanceCount() != null && detail.getAttendanceFaceCount() != null && detail.getAttendanceCount() > 0) {
            return (double) detail.getAttendanceFaceCount() / detail.getAttendanceCount();
        }
        return 0;
    }
}
